package com.example.notepad;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.notepad.adapter.NoteListAdapter;
import com.example.notepad.bean.NotePad;
import com.example.notepad.db.MyDbHelper;
import com.example.notepad.ui.EditActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    // 数据库帮助类
    public MyDbHelper myDbHelper;

    // 用于展示笔记列表的RecyclerView
    private RecyclerView recyclerView;
    private NoteListAdapter noteListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化数据库帮助类
        myDbHelper = new MyDbHelper(this);

        //设置添加按钮的点击事件
        findViewById(R.id.addIV).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, EditActivity.class));
            }
        });

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.listRV);

        // 创建笔记列表适配器
         noteListAdapter = new NoteListAdapter();

        // 设置笔记列表适配器
        recyclerView.setAdapter(noteListAdapter);

        // 设置布局管理器为线性布局
        recyclerView.setLayoutManager(new LinearLayoutManager(this));



        findAll();

    }

    public void findAll() {
        // 从数据库获取所有笔记并更新列表
        List<NotePad> notePadList = myDbHelper.findAll();

        // 更新适配器数据
        noteListAdapter.update(notePadList);
    }
}